package com.church.action;

import java.util.Date;
import java.util.Map;

import com.church.db.Announcement;
import com.church.services.AnnouncementServices;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class EditAnnouncement extends ChurchActionSupport{
	private AnnouncementServices announcementServices;
	private Announcement announcement;
	private boolean success;
	public AnnouncementServices getAnnouncementServices() {
		return announcementServices;
	}

	public void setAnnouncementServices(
			AnnouncementServices announcementServices) {
		this.announcementServices = announcementServices;
	}
	public String execute() {
//		ActionContext actionContext = ActionContext.getContext();
//		Map session = actionContext.getSession();
//		String userGroup = (String) session.get("USER_GROUP");
//		String userType = (String) session.get("USER_TYPE");
//		String userName=(String)session.get("USER_NAME");
//		if (userGroup != null && userType != null) {
//			if (!userGroup.equals("user") && userType.equals("admin")) {
//				announcement.setState("0");
//				announcement.setUser(userName);
//				if(announcement.getDate()==null)
//				{
//					announcement.setDate(new Date(System.currentTimeMillis()));
//				}
//				if(announcement.getGroup()==null)
//				{
//					announcement.setGroup(userGroup);
//				}
//				if (action.equals("update")) {
//					announcementServices.update(announcement);
//				} else {
//					announcementServices.add(announcement);
//				}
//			}
//		}

		return SUCCESS;
	}
	


	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public Announcement getAnnouncement() {
		return announcement;
	}

	public void setAnnouncement(Announcement announcement) {
		this.announcement = announcement;
	}
	public String addAnnouncement()
	{
		if(announcement!=null)
		{
			try {
				String user=this.getUserInfo().getUserName();
				//announcement.setContent(new String(announcement.getContent().getBytes("ISO-8859-1"), "UTF-8"));
				announcement.setUser(user);
				announcement.setState("0");
				success=announcementServices.add(announcement).equals("success");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return SUCCESS;
	}
	public String removeAnnouncement()
	{
		int id=announcement.getId();
		announcement = announcementServices.getAnnouncementById(id);
		announcement.setState("3");
		success=announcementServices.update(announcement).equals("success");
		return SUCCESS;
	}
}
